package showtime.framework.utils
{
	import flash.display.InteractiveObject;
	import flash.display.Stage;
	import flash.events.ContextMenuEvent;
	import flash.ui.ContextMenuItem;
	
	public final class ExceptionTracker
	{
		private static var initialized:Boolean = false;
		/**
		 * Application stage. 
		 */		
		private static var root:Stage = null;
		
		private static var content:ExceptionContent = null;
		
		private static var contextItem:ContextMenuItem = null; 
		
		private static var visible:Boolean = false;
		
		static public function initialize(stage:Stage, context:InteractiveObject):void
		{
			if (initialized)
			{
				return;
			}
			
			initialized = true;
			root = stage;
			content = new ExceptionContent();

			contextItem = new ContextMenuItem("Show ETraker", true);
			contextItem.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, menuItemSelectHandler);
			
			context.contextMenu.customItems.push(contextItem);
		}
		
		private static function menuItemSelectHandler(e : ContextMenuEvent) : void 
		{
			if (!visible)
			{
				show();
			} 
			else
			{
				hide();
			}
		}
		
		private static function show():void
		{
			contextItem.caption = "Hide ETraker";
			visible = true;
			root.addChild(content);
		}
		
		private static function hide():void
		{
			contextItem.caption = "Show ETraker";
			visible = false;
			content.clearException();
			root.removeChild(content);
		}
		
		//////////////////////////////////////
		//			常用API。
		////////////////////////////////////
		
		/**
		 * DEBUG Level指出细粒度信息事件对调试应用程序是非常有帮助的。 
		 * @param message
		 * 
		 */		
		static public function debug(message:String):void
		{
			content.appendException("<font color='#FFFFFF'>>>" + message + "</font>");
		}
		
		/**
		 * INFO level表明 消息在粗粒度级别上突出强调应用程序的运行过程 
		 * 
		 */		
		static public function info(message:String):void
		{
			
		}
		
		/**
		 * WARN level表明会出现潜在错误的情形。 
		 * @param message
		 * 
		 */		
		static public function warn(message:String):void
		{
			
		}
		
		/**
		 * ERROR level指出虽然发生错误事件，但仍然不影响系统的继续运行。 
		 * 
		 */		
		static public function error(message:String):void
		{
			content.appendException("<font color='#FF0000'>>>" + message + "</font>");
		}
		
		/**
		 * FATAL level指出每个严重的错误事件将会导致应用程序的退出。 
		 * 
		 */		
		static public function fatal(message:String):void
		{
			
		}
	}
}


import flash.display.Sprite;
import flash.events.Event;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;

internal class ExceptionContent extends Sprite
{
	private var messageTxt:TextField = null;
	
	public function ExceptionContent():void
	{
		mouseEnabled = false;		
		
		messageTxt = new TextField();
		messageTxt.multiline = true;
		messageTxt.autoSize = TextFieldAutoSize.LEFT;
		messageTxt.mouseEnabled = false;
		addChild(messageTxt);
		addEventListener(Event.ADDED_TO_STAGE, addedToStageHandler, false, 0, true);
		
		addEventListener(Event.REMOVED_FROM_STAGE, remvoedToStageHandler, false, 0, true);
	}
	
	public function appendException(message:String):void
	{
		messageTxt.htmlText += message + "<br/>";
	}
	
	public function clearException():void
	{
		messageTxt.htmlText = "";
	}
	private function addedToStageHandler(evt:Event):void
	{
		resizeHadnler(evt);
		stage.addEventListener(Event.RESIZE, resizeHadnler);
		
	}
	
	private function remvoedToStageHandler(evt:Event):void
	{
		stage.removeEventListener(Event.RESIZE, resizeHadnler);
		
	}
	
	private function resizeHadnler(evt:Event):void
	{
		graphics.clear();
		graphics.beginFill(0x000000,0.5);
		graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight * 0.5);
		messageTxt.width = stage.stageWidth;
		messageTxt.height = stage.stageHeight * 0.5;
		
		graphics.endFill();
	}
}